package com.sctvcloud.bazhong.ui.utils;

import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.ijkplaylib.utils.PlayerUtils;
import com.ruihang.ijkplaylib.widget.GPlayerView;
import com.ruihang.ijkplaylib.widget.IGPlayerBottomControll;
import com.ruihang.ijkplaylib.widget.IjkVideoView;
import com.sctvcloud.bazhong.R;

import java.lang.ref.WeakReference;

/**
 * Created by liuyu on 2017/8/3.
 */

public class NewsAudioBottomControllerImp implements IGPlayerBottomControll, View.OnClickListener {

    private WeakReference<GPlayerView> playerViewWea;

    protected View base;

    protected ImageView playBtn;

    private CustomFontTextView playedTime;

    protected boolean isPortrait;

    private boolean isPlaying;

    public NewsAudioBottomControllerImp(View base) {
        this.base=base;
        initViews();
    }

    private void initViews() {
        playBtn= (ImageView) base.findViewById(R.id.news_detail_audio_play);
        playBtn.setOnClickListener(this);
        playedTime= (CustomFontTextView) base.findViewById(R.id.news_detail_audio_time_played);
    }

    @Override
    public void onProgressSlide(long delta) {
    }

    @Override
    public boolean canChangeScreen() {
        return true;
    }

    @Override
    public void onUpdateUI(boolean isPlaying, boolean isLiving, boolean isPortrait) {
        this.isPortrait = isPortrait;
        this.isPlaying=isPlaying;
        if (isPlaying) {
            playBtn.setImageResource(R.mipmap.icon_home_onair);
            GPlayerView playerView=getGPlayerTemp();
            if (playerView!=null){
                playerView.setAutoPlay(false);
            }
        } else {
            playBtn.setImageResource(R.mipmap.icon_home_play);
        }
    }

    @Override
    public long syncProgress(IjkVideoView videoView) {
        if (videoView == null) {
            return 0L;
        }
        long position = videoView.getCurrentPosition();
        long duration = videoView.getDuration();
//        if (seekBar != null) {
//            if (duration > 0) {
//                long pos = 1000L * position / duration;
//                seekBar.setProgress((int) pos);
//            }
//            int percent = videoView.getBufferPercentage();
//            seekBar.setSecondaryProgress(percent * 10);
//        }
        if (playedTime!=null) {
            playedTime.setText(PlayerUtils.generateTime(position));
        }
//        totalTime.setText(PlayerUtils.generateTime(duration));
        return position;
    }

    @Override
    public void setHasBarrage(boolean hasBarrage) {
    }

    @Override
    public void onAddingView(GPlayerView playerView, LayoutInflater inflater, ViewGroup group) {
        playerViewWea=new WeakReference<GPlayerView>(playerView);
    }

    @Override
    public boolean onShowPanl(boolean isPortrait, boolean isLive, boolean isPlaying) {
        return false;
    }

    @Override
    public void onHidePanl(boolean isPortrait, boolean isLive, boolean isPlaying) {
    }

    @Override
    public void onPortrait() {
    }

    @Override
    public void onLandscape() {
    }

    @Override
    public void onDetach(ViewGroup group) {
    }

    @Override
    public void onDestory() {
        if (playBtn!=null){
            playBtn.setOnClickListener(null);
        }
        base=null;
        playBtn=null;
        playedTime=null;
    }

    @Override
    public void onPlayerHide(GPlayerView playerView) {
    }

    @Override
    public void onClick(View v) {
        GPlayerView playerView=getGPlayerTemp();
        if (playerView!=null){
            if (isPlaying){
                playerView.pausePlay();
            }else {
                playerView.startPlay();
            }
        }
    }

    @Nullable
    private GPlayerView getGPlayerTemp(){
        if (playerViewWea!=null){
            return playerViewWea.get();
        }
        return null;
    }
}
